package com.example.demo.modules.customDefine.controller;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.customDefine.entity.ModuleField;
import com.example.demo.modules.customDefine.service.ModuleFieldService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import static com.example.demo.common.util.Utils.isNil;
import static com.example.demo.common.util.Utils.uuidSnow;

@RestController
@RequestMapping("/api/moduleField")
public class ModuleFieldController {

    @Autowired
    private ModuleFieldService moduleFieldService;


    @GetMapping("/listAll")
    public R listAll(@RequestParam(value = "moduleName", required = true) String moduleName) {
        QueryWrapper<ModuleField> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("module_name", moduleName);
        return R.ok().put("result", moduleFieldService.listAllWithFieldTypeName(queryWrapper));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", dataType = "String")
    })
    public R page(
            Integer pageSize, Integer pageNum) {
        if (isNil(pageSize)) {
            pageSize = 10;
        }
        if (isNil(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<ModuleField> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result", moduleFieldService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @PostMapping("/add")
    public R add(@RequestBody ModuleField moduleField) {
        /*
         * @flow
         * uuid_key存在 判断  uuid_key 是不是唯一的
         * uuid_key 不存在 新生成一个uuid;
         * */
        if(null != moduleField.getUuidKey() && !("".equals(moduleField.getUuidKey()))){
            QueryWrapper<ModuleField> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid_key", moduleField.getUuidKey());
            ModuleField one = moduleFieldService.getOne(queryWrapper);
            //存在就不能继续添加
            if(null != one){
                return R.error().put("msg", one.getUuidKey()+"已经存在");
            }
        }else{
            moduleField.setUuidKey("key_"+uuidSnow());
        }

        return R.ok().put("result", moduleFieldService.save(moduleField));
    }

    @PostMapping("/update")
    public R update(@RequestBody ModuleField moduleField, HashMap<String, Object> updateOptions) {
        QueryWrapper<ModuleField> queryWrapper = new QueryWrapper<>();
        if (!isNil(moduleField.getId())) {
            queryWrapper.eq("id", moduleField.getId());
        } else {
            return R.error("id不能为空");
        }
        return R.ok().put("result", moduleFieldService.update(moduleField, queryWrapper));
    }

    @GetMapping("/delete")
    public R deleteOne(@RequestParam(value = "id", required = true) String id) {
        return R.ok().put("result", moduleFieldService.removeById(Convert.toInt(id)));
    }
}